// pages/my/qrcode/newcode.js

const App = getApp()
const api = require('../../../utils/api.js');
var util = require('../../../utils/util.js');

var interval;//定时器

Page({
  data: {
    flow_no: null,
    security_code: null,
    money: '',
  },

  onLoad(options) {
    this.setData({
      money: options.money
    })



    const that = this

    //先调用一次
    this.getQrCode(options)
    //定时器
    interval = setInterval(function () {
      console.log("定时器" + interval)
      //每隔一分钟定时调用  付款码条码:
      that.getQrCode(options)
    }, 1000 * 60)
  },


  // 生命周期函数--监听页面卸载
  onUnload: function () {
    clearInterval(interval);//页面卸载时 消毁定时器
  },





  getQrCode: function (e) {

    const data = {
      url_type: '/openapi/trade/reg',

      app_token: App.globalData.app_token,         //add
      security_code: App.globalData.security_code  //add

    }

    api.reqData({
      data,
      success: (res) => {

        var resData = res.data;
        console.log(resData);

        if (resData.rsp_code != 'fail') {
          this.setData({
            flow_no: res.data.data.flow_no,
            security_code: res.data.data.security_code
          })

          const paramsData = {
            amount: e.money,
            type: 'P',
            member_type_key: 'consumer',
            member_id: App.globalData.memberInfo.member_id,
            security_code: res.data.data.security_code,
            flow_no: res.data.data.flow_no,
          }

          console.log(">>>paramsData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
          console.log(JSON.stringify(paramsData))

          util.showQrcode('qrcode', JSON.stringify(paramsData), 350, 350);

        }
      },
      fail: (res) => {
        console.log(res.data);
      }
    })
  },

})